(function ($) {
    $.fn.pull_to_refresh = function (options) {
        var defaults = {
            refresh: function (callback) { },
            pull_to_refresh_text: translate('PullToRefresh_PullDown'),
            letgo_text: translate('PullToRefresh_Release'),
            refreshing_text: translate('PullToRefresh_Refreshing'),
            status_indicator_id: 'pull_to_refresh',
            refresh_class: 'refresh',
            visible_class: 'visible'
        };

        var options = $.extend(defaults, options);

        var content,
		loadImg,
        status_indicator,
        refreshing,
        contentStartY = 0,
        startY = 0,
        track = false,
        refresh = false;

        content = $(this);
        var now = new Date();
        var html = '';
        html += '<div class="loader pull ' + options.status_indicator_id + '">';
        html += '<div class="loader-pull"><div class="spinner-light floatleft">';
        html += '<img class="loadImg" src="' + getImageUrl('loader-arrow-dn.png') + '" width="50" height="50" /></div>';
        html += '<div class="pull-check floatleft"><span class="lastUpdate">' + translate('PullToRefresh_LastUpdate') + ' ' + moment().format("h.mm a") + '</span><div class="pull-text">';
        html += options.pull_to_refresh_text + '</div></div><div class="clear"></div></div></div>';
        content.prepend(html);
        status_indicator = content.find('.' + options.status_indicator_id);
        loadImg = content.find('.loadImg');

        var begun = false;

        document.body.addEventListener('touchstart', function (e) {
            if (!status_indicator.hasClass('active') || (app.LeftMenu.instance && app.LeftMenu.instance.isVisible()))
                return;

            try {
                contentStartY = parseInt(status_indicator.css('margin-top'));
            }
            catch (e) {
                contentStartY = 0;
            }
            startY = e.touches[0].screenY;
        });

        var hidePullDown = function () {
            status_indicator.css('margin-top', '-53px').removeClass(options.refresh_class);
            status_indicator.find(".pull-text").text(options.pull_to_refresh_text);
            loadImg.attr('src', getImageUrl('loader-arrow-dn.png'));
            begun = false;
        };

        $(document).bind('hidePullDown', function () { hidePullDown(); });

        document.body.addEventListener('touchend', function (e) {
            if (app.CurrentPage && app.CurrentPage.instance && app.CurrentPage.instance.isAnyDialogOpen())
                return;

            if (!status_indicator.hasClass('active'))
                return;

            begun = false;
            if (refresh) {
                now = new Date();
                status_indicator.find(".pull-text").text(options.refreshing_text);
                status_indicator.find(".lastUpdate").text(translate('PullToRefresh_LastUpdate') + ' ' + moment().format("h.mm a"));
                loadImg.attr('src', getImageUrl('spinner-light-anim.gif'));
                $(document).trigger('pullDown');
                refresh = false;
            } else if (track) {
                hidePullDown();
            }

            track = false;
        });

        document.body.addEventListener('touchmove', function (e) {
            if (app.CurrentPage && app.CurrentPage.instance && app.CurrentPage.instance.isAnyDialogOpen())
                return;
            if ((app.LeftMenu.instance && app.LeftMenu.instance.isVisible()))
                return;

            if (!status_indicator.hasClass('active'))
                return;

            if (begun && $(window).scrollTop() > 1) {
                begun = false;
                return;
            }
            if (!begun && $(window).scrollTop() > 1)
                return;

            if (startY - e.changedTouches[0].screenY > 0)
                return;

            var move_to = contentStartY - (startY - e.changedTouches[0].screenY);
            if (move_to > 0)
                track = true;
            move_to = Math.min(move_to, 0);
            status_indicator.css('margin-top', move_to + 'px');
            begun = true;
            e.preventDefault();

            if (move_to == 0) {
                refresh = true;
                status_indicator.addClass(options.visible_class)
                status_indicator.find(".pull-text").text(options.letgo_text)
                loadImg.attr('src', getImageUrl('loader-arrow-up.png'));
            } else refresh = false;
        });

        hidePullDown();
    }
})(jQuery);